package com.uenpay.im.invokeitems.login;

import android.util.Log;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.utilities.DateUtils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.serializations.QYXUserSerializer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 登录
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class RegisterInvokeItme extends HttpInvokeItem {


	public class RegisterInvokeItmeResult {
		public int status;
		public String msg;
		public QYXUserEntity mQYXUserEntity = new QYXUserEntity();
	}

	public RegisterInvokeItmeResult getOutput() {
		return (RegisterInvokeItmeResult) GetResultObject();
	}

	public RegisterInvokeItme(String mobile_phone, String pwd,String nickname,
							  String phone_validate_code) {
		String time = DateUtils.getCurrentSeconds() + "";
		String url = APIConfiguration.getBaseUrl() + "Cust/Init/register?";
		HashMap<String, String> mRequestParams = new HashMap<String, String>();
		mRequestParams.put("username", mobile_phone);
		mRequestParams.put("password", pwd);
		mRequestParams.put("nickname", nickname);
		mRequestParams.put("vercode", phone_validate_code);
		mRequestParams.put("time", time);
		mRequestParams.put("sign",
				Utils.md5(Utils.md5(mobile_phone + "_" + time)));
		SetRequestParams(mRequestParams);
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		RegisterInvokeItmeResult result = new RegisterInvokeItmeResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			if (json.has("data")) {
				JSONObject js = json.optJSONObject("data");
				if (js == null) {
					return result;
				}
				IMApplication.getInstance().setLoginInfo(
						js.optString("custid"), js.optString("token"),js.optString("auth"));
				result.mQYXUserEntity = QYXUserSerializer.deserializeUser(js);
			}

		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("JSONException:", ""+e.toString());
		}
		return result;
	}


}
